#include "starSystem.h"


starSystem::starSystem() {

    MapX=0;
    MapY=0;
    ScreenX=0;
    ScreenY=0;
    running = 0;
    StarsCNT = 0;

    // Pointer
    sfSmall  = NULL;
    sfMedium = NULL;
    sfBig  = NULL;

}

starSystem::~starSystem() {
	destroy_bitmap(sfSmall);
	destroy_bitmap(sfMedium);
	destroy_bitmap(sfBig);
}




//***** move the stars *****
void starSystem::Calculate(int pFactor) {

    if(running) {

        int mod=0, i;
        float playerX, playerY;

        playerX = 1.1;
        playerY = 0.6;

        for(i=1;i<StarsCNT;i++) {

            // Floppen
            if(Stars[i].y <    0) Stars[i].y = MapY;
            if(Stars[i].x <    0) Stars[i].x = MapX;
            if(Stars[i].y > MapY) Stars[i].y = 0;
            if(Stars[i].x > MapX) Stars[i].x = 0;

            // Ebenen modifikator
            if(Stars[i].typ==3) mod = 15;
            if(Stars[i].typ==2) mod = 6;
            if(Stars[i].typ==1) mod = 2;

            Stars[i].x -= (playerX/mod)*pFactor;
            Stars[i].y -= (playerY/mod)*pFactor;

        }
    }
}




//***** draw the stars *****
int starSystem::Draw(void) {

    if(running) {

        int i, x, y;

        for(i=1;i<StarsCNT;i++) {
            x = (int)Stars[i].x;
            y = (int)Stars[i].y;
            if(x>0 && x<ScreenX && y>0 && y<ScreenY) {
                if(Stars[i].typ==3) masked_blit(sfSmall, screen, 0, 0, x, y, sfSmall->w, sfSmall->h);
                else if(Stars[i].typ==2) masked_blit(sfMedium, screen, 0, 0, x, y, sfMedium->w, sfMedium->h);
                else masked_blit(sfBig, screen, 0, 0, x, y, sfBig->w, sfBig->h);
            }
        }
    }
    return StarsCNT;
}





//***** create the stars *****
void starSystem::Init(int pIntensity) {
    int i;
    structStar temp;

    // start random generator
    srand(time(NULL));


    MapX = screen->w*2;
    MapY = screen->h*2;

    ScreenX = screen->w;
    ScreenY = screen->h;

    // loadBitmaps
    BITMAP *tmp;

    tmp = load_bitmap("GFX/bg/mapStarKlein.bmp", NULL);
    if(!tmp) {
        cout << "load_bitmap: Could not load [GFX/bg/mapStarKlein.bmp]" << endl;
        exit(2);
    }
    sfSmall = create_video_bitmap(tmp->w, tmp->h);
    blit(tmp, sfSmall, 0, 0, 0, 0, tmp->w, tmp->h);
    if(!sfSmall) {
        cout << "create_video_bitmap: Could create [sfSmall]" << endl;
        exit(2);
    }


    tmp = load_bitmap("GFX/bg/mapStarMittel.bmp", NULL);
    sfMedium = create_video_bitmap(tmp->w, tmp->h);
    blit(tmp, sfMedium, 0, 0, 0, 0, tmp->w, tmp->h);


    tmp = load_bitmap("GFX/bg/mapStarGross.bmp", NULL);
    sfBig = create_video_bitmap(tmp->w, tmp->h);
    blit(tmp, sfBig, 0, 0, 0, 0, tmp->w, tmp->h);


    destroy_bitmap(tmp);


    // type close
    for(i=0;i<=(pIntensity*25);i++) {
        temp.typ = 3;
        temp.x = rand()%MapX;
        temp.y = rand()%MapY;
        Stars.push_back(temp);
        StarsCNT++;
    }

    // type mittle
    for(i=0;i<=(pIntensity*20);i++) {
        temp.typ = 2;
        temp.x = rand()%MapX;
        temp.y = rand()%MapY;
        Stars.push_back(temp);
        StarsCNT++;
    }

    // type far
    for(i=0;i<=(pIntensity*10);i++) {
        temp.typ = 1;
        temp.x = rand()%MapX;
        temp.y = rand()%MapY;
        Stars.push_back(temp);
        StarsCNT++;
    }

    // start the rendering
    running = 1;

}
